package com.example.zzpneo4j.controller;

import com.example.zzpneo4j.config.MinioUtil;
import com.example.zzpneo4j.dto.Result;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * 文件管理
 * <p>使用minio作为文件管理工具</p>
 *
 * @author zzp
 */
@RestController
@RequestMapping("file")
public class FileController {
    @Resource
    MinioUtil minioUtil;

    /**
     * 文件上传
     *
     * @param file
     * @return
     * @throws IOException
     */
    @PostMapping("upload")
    public Result upload(MultipartFile file) throws IOException {
        String code = minioUtil.uploadFile(file.getInputStream(), file.getOriginalFilename());
        Map<String, Object> hashMap = new HashMap<>();
        hashMap.put("fileName", file.getOriginalFilename());
        hashMap.put("fileCode", code);
        hashMap.put("filePath", "undefined");
        return Result.success(hashMap);
    }

    @GetMapping("download/{fileCode}")
    public void download(HttpServletResponse response, @PathVariable String fileCode) {
        minioUtil.lookUploadFile(response, fileCode, fileCode);
    }

}
